package com.example.su_shi_tan_bas_lib.view.video_view;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.library_common.view.video_view.utils.VideoBean;
import com.example.su_shi_tan_bas_lib.R;
import com.example.su_shi_tan_bas_lib.utils.QMUIDisplayHelper;
import com.example.su_shi_tan_bas_lib.view.video_view.utils.VideoScreenSwitchManager;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.ui.AspectRatioFrameLayout;
import com.google.android.exoplayer2.ui.PlayerControlView;

import static com.google.android.exoplayer2.ui.PlayerView.SHOW_BUFFERING_NEVER;

public class CustomVideoView extends FrameLayout {

    private CustomVideoViewPlayer mPlayerView;

    private VideoBean videoBean = new VideoBean("", "");

    private View mVideoViewRenderContainer;

    private int mVideoViewRenderContainerId;

    private int primaryWidth;

    private int primaryHeight;


    public CustomVideoView(@NonNull Context context) {
        this(context, null);
    }

    public CustomVideoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomVideoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.custom_video_view, this);

        mVideoViewRenderContainer = findViewById(R.id.mVideoViewRenderContainer);

        mVideoViewRenderContainerId = mVideoViewRenderContainer.getId();

        mPlayerView = findViewById(R.id.mPlayerView);

        handlePlayerSizeMode();


    }

    public void onPlayingLayoutVisibly(boolean visible) {
        mPlayerView.onPlayingLayoutVisibly(visible);
    }

    private void handlePlayerSizeMode() {
        if (QMUIDisplayHelper.isPortrait()) {
            mPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
        } else if (QMUIDisplayHelper.isLandscape()) {
            mPlayerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (primaryHeight <= 0) {
            primaryHeight = MeasureSpec.getSize(heightMeasureSpec);
        }

        if (primaryWidth <= 0) {
            primaryWidth = MeasureSpec.getSize(widthMeasureSpec);
        }

    }

    public long getVideoCurrentPosition() {
        return mPlayerView.getVideoCurrentPosition();
    }


    public long getVideoDuration() {
        return mPlayerView.getVideoDuration();
    }

    public void addVideoListener(Player.EventListener listener) {
        mPlayerView.addVideoListener(listener);
    }

    public boolean isPlaying(){
        return mPlayerView.isPlaying();
    }

//    public void setProgressUpdateListener(@Nullable PlayerControlView.ProgressUpdateListener listener) {
//        mPlayerView.setPlaybackPreparer();
//    }

    public void setProgressUpdateListener(@Nullable PlayerControlView.ProgressUpdateListener listener) {
        mPlayerView.setProgressUpdateListener(listener);
    }

    public View getVideoViewTopController() {
        return mPlayerView.getVideoViewTopController();
    }

    public int getPrimaryHeight() {
        return primaryHeight;
    }

    public int getPrimaryWidth() {
        return primaryWidth;
    }

    //渲染容器
    public View getRenderContainer() {
        return mVideoViewRenderContainer;
    }

    public int getVideoViewRenderContainerId() {
        return mVideoViewRenderContainerId;
    }

    public void setCover(String cover_url) {
        videoBean = videoBean.copy(cover_url, videoBean.getCover_url());
        mPlayerView.setCover(videoBean.getCover_url());
    }

    public void playUrlWithStart(String url) {
        videoBean = videoBean.copy(url, videoBean.getCover_url());
        mPlayerView.playUrlWithStart(videoBean.getVideo_url());
    }


    public void onPause() {
        mPlayerView.onPause();
    }

    public void onResume() {
        if (TextUtils.isEmpty(videoBean.getVideo_url())) {
            return;
        }

        mPlayerView.onResume();
    }

    public void stopPlayer() {
        Player player = mPlayerView.getPlayer();
        mPlayerView.setShowBuffering(SHOW_BUFFERING_NEVER);
        if (player != null) {
            player.release();
        }
    }


    @Override
    protected void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (QMUIDisplayHelper.isLandscape()) {

            VideoScreenSwitchManager.getInstance()
                    .setFinalTaskEvent(this::resolveFinalScreen)
                    .toChangLandscape(this);

        } else if (QMUIDisplayHelper.isPortrait()) {

            VideoScreenSwitchManager.getInstance()
                    .setFinalTaskEvent(this::resolveFinalScreen)
                    .toChangPortrait(this);

        }


    }

    private void resolveFinalScreen() {
        handlePlayerSizeMode();
        mPlayerView.startVideo(videoBean.getVideo_url());

    }

    public boolean onBackPress() {
        if (QMUIDisplayHelper.isLandscape()) {
            QMUIDisplayHelper.setPortrait((Activity) getContext());
            return true;
        }
        return false;
    }


}
